package org.eclipse.php.internal.console.ui.util;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;

public class Util {

	/**
	 * We will initialize file contents with a sample text.
	 */
	public static InputStream openContentStream(String contents) {
		if (contents == null) {
			contents = ""; //$NON-NLS-1$
		}

		return new ByteArrayInputStream(contents.getBytes());
	}

	public static void setFileContent(IFile file, String contents,
			IProgressMonitor monitor) throws CoreException, IOException {
		InputStream stream = Util.openContentStream(contents);
		if (file.exists()) {
			file.setContents(stream, true, true, monitor);
		} else {
			file.create(stream, true, monitor);
		}
		stream.close();
	}

}
